package org.kukker;

import java.util.ArrayList;

import org.kukker.R;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Telephony;
import android.sax.StartElementListener;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.util.Log;

import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnKeyListener;
import android.widget.*;

public class SmsIntentReceiver extends IntentReceiver{
	private String id = "Kukker";
	

	@Override
	public void onReceiveIntent(Context context, Intent intent) {
		
		ArrayList<SmsMessage> smses = SmsManager.getDefault().getAllMessagesFromSim();
		int numSms = smses.size();
		Log.i("SMS","numSMS:"+numSms+" ");
		
		
		try{
			String name = intent.getAction();
			Log.i("Kukker","onReceiveIntent: Name:"+name);
		
		            StringBuilder buf = new StringBuilder();
		            Bundle bundle = intent.getExtras();
		            if (bundle != null) {
		            	
		                SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
		                for (int i = 0; i < messages.length; i++) {
		                    SmsMessage message = messages[i];
		                    
		                    String emailBody = message.getEmailBody();
		                    String emailFrom = message.getEmailFrom();
		                    int indexOnSim = message.getIndexOnSim();
		                    String messageBody = message.getMessageBody();
		                    int statusOnSim = message.getStatusOnSim();
		                    long timeStamp = message.getTimestampMillis();
		                    String displayMessageBody = message.getDisplayMessageBody();
		                    String displayOriginatingAddress = message.getDisplayOriginatingAddress();
		                   
		                    int[] len = SmsMessage.calculateLength(displayMessageBody);		                                        
		                    
		                    Log.i("SMS","emailbody:"+emailBody+" ");
		                    Log.i("SMS","emailfrom:"+emailFrom+" ");
		                    Log.i("SMS","indexonsim:"+indexOnSim+" ");
		                    Log.i("SMS","statusonsim:"+statusOnSim+" ");
		                    Log.i("SMS","timestamp:"+timeStamp+" ");
		                    Log.i("SMS","messagebody:"+displayMessageBody+" ");
		                    Log.i("SMS","originaddress:"+displayOriginatingAddress+" ");		                    
		                    Log.i("SMS","len.length:"+len.length+" ");
		                    Log.i("SMS","len[0]:"+len[0]+" ");
		                    Log.i("SMS","len[1]:"+len[1]+" ");
		                    Log.i("SMS","len[2]:"+len[2]+" ");
		                    Log.i("SMS","len[3]:"+len[3]+" ");		                    
		                    
		                }
		            }
		            
		            
		            
		            //NotificationManager nm = (NotificationManager) context.getSystemService(
		              //      Context.NOTIFICATION_SERVICE);
		            
		            
		           // new Notification(context,1,"ticker",1000,"expandedTicker","expandedText",new Intent(),2,"appName",new Intent());
		            
		            
		            
		            //AlarmManager am = context.getSystemService(Context.ALARM_SERVICE);
		                       
		           
		            //nm.notifyWithText(123, buf.toString(), "1000", null);

		        

			
		
			
			
			/*
			Bundle bundle = intent.getBundleExtra("info");		
			int chain = bundle.getInt("chain");
			int ttl = bundle.getInt("ttl");
			
			String extra = bundle.getString("org.kubus.kubus.intent.POKE.extra");
			String chainInfo = "chain="+chain+":ttl="+ttl+":extra="+extra;
			String actionInfo = "action="+intent.getAction();
			*/
			
			
			//Toast.makeText(context, id+":"+intent.getAction(), Toast.LENGTH_SHORT).show();
			
			/*
			try{
				//String cn = cp.getClassName();
				//Toast.makeText(context, id+":className="+cp.getClassName(), Toast.LENGTH_SHORT).show();
			}catch(Exception e){
				Toast.makeText(context, "error:"+e.getMessage(), Toast.LENGTH_SHORT).show();
				
			}
			*/
			
		}
		catch(Exception e){
			//Toast.makeText(context,"Error="+e.getMessage(), Toast.LENGTH_SHORT).show();
			Log.e(id,e.getMessage());
		}
	} 
  
    
}